package server.server;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class User {
    private String name;
    private long uid;
    private final Socket s;
    private int privilege; // 特权级
    private boolean isSilent; // 是否被禁言

    public User(Socket s) {
        this.s = s;
        this.name = "张三";
        this.uid = 1;
        this.privilege = 2;
        this.isSilent = false;
    }

    public boolean isSilent() {
		return isSilent;
	}

	public void setSilent(boolean isSilent) {
		this.isSilent = isSilent;
	}

	public int getPrivilege() {
        return privilege;
    }

	public void setPrivilege(int privilege) {
		this.privilege = privilege;
	}

	public long getUID() {
        return uid;
    }

    // 测试用函数，UID应固定
    public void setUID(long uid) {
        this.uid = uid;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Socket getSocket() {
        return s;
    }

    public void recv(String msg) {
        if (!msg.isEmpty()) {
            try {
                OutputStream outToServer = s.getOutputStream();
                DataOutputStream out = new DataOutputStream(outToServer);
                out.write(msg.getBytes(StandardCharsets.UTF_8));
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    // 关闭socket，用于踢人
    public void shutdown() {
        try {
			s.close();
		} catch (IOException e) {
			//Do nothing
		}
    }
}
